/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.incremental;

import java.awt.Dialog;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import javax.swing.DefaultCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
import syntax.Clock;
import syntax.ClockConditionAtom;
import syntax.InternalAction;
import syntax.Location;
import syntax.Message;
import syntax.Repository;
import syntax.Transition;

/**
 *
 * @author ennouaary
 */
public class JTransitionDialog extends TIOAObservableJDialog {

    private Transition origTransition, transition;
    Repository repository;
    InternalAction internalAction;
    TableModel resetClocksModel;
    TableColumn resetClocksClockColumn, resetClocksStatusColumn;
    Collection<Location> locations;
    Collection<Message> messages;
    Collection<Clock> clocks;
    Collection<Transition> transitions;
    TransitionObservable observable;
    boolean newNotEdit = true;
    Location srcLocation, destLocation;
    Message message;
    ClockConditionAtom clockGuard;

    /**
     * Creates new form JTransitionDialog
     */
    public JTransitionDialog(TransitionObservable observable, Repository repository) {
        this.transition = new Transition("");
        this.observable = observable;
        this.repository = repository;
        internalAction = repository.getInternalAction();
        newNotEdit = true;
        initCollections();
        initComponents();
        fillInComponents();
        jActionLabel.setText("New Transition");
    }

    private void restore() {
        this.transition = (Transition) (origTransition.clone());
        initCollections();
        fillInComponents();
    }

    public JTransitionDialog(Transition transition, TransitionObservable observable, Repository repository) {
        this.origTransition = transition;
        this.observable = observable;
        this.repository = repository;
        internalAction = repository.getInternalAction();
        newNotEdit = false;
        initComponents();
        restore();
        jActionLabel.setText("Edit Transition");
    }

    private void initCollections() {
        locations = repository.getAllLocations();
        messages = repository.getAllMessages();
        clocks = repository.getAllClocks();
        transitions = repository.getAllTransitions();
    }

    private void fillInComponents() {
        int infinity = (int) Math.floor(parameters.Parameters.getInfinity());
//        jSourceLocationComboBox.setModel(null);
        fillInClockGuardAtoms();
        fillInMessages();
        fillInSourceLocations();
        fillInDestinationLocations();
        fillInResetClocks();
        jNameTextField.setText(transition.getName());
    }

    private void fillInClockGuardAtoms() {
        jClockGuardAtomsComboBox.removeAllItems();
        List<ClockConditionAtom> atoms;
        atoms = transition.getClockGuard().getConditionAtom();
        Collections.sort(atoms);
        //Collections.reverse(atoms);
        for (ClockConditionAtom atom : atoms) {
            jClockGuardAtomsComboBox.addItem(atom);
        }
        if (transition.getClockGuard().getConditionAtom().isEmpty()) {
            jClockGuardEditButton.setEnabled(false);
            jClockGuardDeleteButton.setEnabled(false);
        } else {
            jClockGuardEditButton.setEnabled(true);
            jClockGuardDeleteButton.setEnabled(true);
        }
    }

    private void fillInMessages() {
//        fillInObjects(jMessageComboBox, repository.getAllMessages(), transition.getMessage());
        fillInObjects(jMessageComboBox, messages, transition.getMessage());
//        if (repository.getAllMessages().size() <= 1) {
        if (messages.size() <= 1) {
            jMessageDeleteButton.setEnabled(false);
            jMessageEditButton.setEnabled(false);
        } else {
            jMessageDeleteButton.setEnabled(true);
            jMessageEditButton.setEnabled(true);
        }
//        Collections.sort((List<Message>) messages);
    }

    private void fillInSourceLocations() {
//        fillInObjects(jSourceLocationComboBox, repository.getAllLocations(), transition.getSrcLocation());
        fillInObjects(jSourceLocationComboBox, locations, transition.getSrcLocation());
//        Collections.sort((List<Location>) locations);
    }

    private void fillInDestinationLocations() {
//        fillInObjects(jDestinationLocationComboBox, repository.getAllLocations(), transition.getDestLocation());
        fillInObjects(jDestinationLocationComboBox, locations, transition.getDestLocation());
//        Collections.sort((List<Location>) locations);
    }

    private void fillInObjects(JComboBox combo, Collection collection, Object selected) {
        List objects = new LinkedList(collection);
        Collections.sort(objects);
        combo.removeAllItems();
        for (Object object : objects) {
            combo.addItem(object);
        }
        combo.setSelectedItem(selected);
    }

    private void fillInResetClocks() {
//        resetClocksModel = new ResetClocksTableModel(repository.getAllClocks().size());
        List<Clock> clockList = new LinkedList<Clock>(clocks);

        Collections.sort((List<Clock>) clockList);
        resetClocksModel = new ResetClocksTableModel(clocks.size());
        int i = 0;
//        for (Clock clock : repository.getAllClocks()) {
        for (Clock clock : clockList) {
            resetClocksModel.setValueAt(clock, i, ResetClocksTableModel.CLOCK);
            resetClocksModel.setValueAt(new Boolean(transition.getResetClocks().contains(clock)), i, ResetClocksTableModel.STATUS);
            i++;
        }
        jResetClocksTable.setModel(resetClocksModel);
        DefaultTableCellRenderer dtcr = new DefaultTableCellRenderer();
        dtcr.setHorizontalAlignment(SwingConstants.CENTER);
        jResetClocksTable.getColumnModel().getColumn(ResetClocksTableModel.CLOCK).setCellRenderer(dtcr);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jClockResetButtonsPanel = new javax.swing.JPanel();
        jOkButton = new javax.swing.JButton();
        jRestoreButton = new javax.swing.JButton();
        jCancelButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jSourceLocationComboBox = new javax.swing.JComboBox();
        jSourceLocationLabel = new javax.swing.JLabel();
        jDestinationLocationLabel = new javax.swing.JLabel();
        jDestinationLocationComboBox = new javax.swing.JComboBox();
        jResetClocksLabel = new javax.swing.JLabel();
        jMessageComboBox = new javax.swing.JComboBox();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jResetClocksTable = new javax.swing.JTable();
        jBlankLabel1 = new javax.swing.JLabel();
        jBlankLabel2 = new javax.swing.JLabel();
        jBlankLabel3 = new javax.swing.JLabel();
        jBlankLabel4 = new javax.swing.JLabel();
        jClockGuardAtomsLabel = new javax.swing.JLabel();
        jClockGuardAtomsComboBox = new javax.swing.JComboBox();
        jMessageLabel = new javax.swing.JLabel();
        jMessageButtonsPanel = new javax.swing.JPanel();
        jMessageNewButton = new javax.swing.JButton();
        jMessageEditButton = new javax.swing.JButton();
        jMessageDeleteButton = new javax.swing.JButton();
        jDestinationLocationButtonsPanel = new javax.swing.JPanel();
        jDestLocationNewButton = new javax.swing.JButton();
        jDestLocationEditButton = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jSourceLocationButtonsPanel = new javax.swing.JPanel();
        jButton7 = new javax.swing.JButton();
        jSrcLocationEditButton = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();
        jClockGuardsButtonPanel = new javax.swing.JPanel();
        jClockGuardNewButton = new javax.swing.JButton();
        jClockGuardEditButton = new javax.swing.JButton();
        jClockGuardDeleteButton = new javax.swing.JButton();
        jActionLabel = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jNameTextField = new javax.swing.JTextField();

        setModal(true);
        setPreferredSize(new java.awt.Dimension(800, 333));
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jClockResetButtonsPanel.setLayout(new java.awt.GridLayout(1, 0));

        jOkButton.setText("OK");
        jOkButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jOkButtonActionPerformed(evt);
            }
        });
        jClockResetButtonsPanel.add(jOkButton);

        jRestoreButton.setText("Restore");
        jRestoreButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRestoreButtonActionPerformed(evt);
            }
        });
        jClockResetButtonsPanel.add(jRestoreButton);

        jCancelButton.setText("Cancel");
        jCancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCancelButtonActionPerformed(evt);
            }
        });
        jClockResetButtonsPanel.add(jCancelButton);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 4;
        getContentPane().add(jClockResetButtonsPanel, gridBagConstraints);

        jPanel1.setLayout(new java.awt.GridBagLayout());

        jSourceLocationComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jSourceLocationComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jSourceLocationComboBoxActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        jPanel1.add(jSourceLocationComboBox, gridBagConstraints);

        jSourceLocationLabel.setText("Source Location");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        jPanel1.add(jSourceLocationLabel, gridBagConstraints);

        jDestinationLocationLabel.setText("Destination Location");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        jPanel1.add(jDestinationLocationLabel, gridBagConstraints);

        jDestinationLocationComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jDestinationLocationComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jDestinationLocationComboBoxActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        jPanel1.add(jDestinationLocationComboBox, gridBagConstraints);

        jResetClocksLabel.setText("Reset Clocks");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 13;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        jPanel1.add(jResetClocksLabel, gridBagConstraints);

        jMessageComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jMessageComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMessageComboBoxActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        jPanel1.add(jMessageComboBox, gridBagConstraints);

        jPanel3.setBackground(new java.awt.Color(204, 204, 204));
        jPanel3.setMaximumSize(new java.awt.Dimension(250, 110));
        jPanel3.setMinimumSize(new java.awt.Dimension(250, 110));
        jPanel3.setPreferredSize(new java.awt.Dimension(250, 110));
        jPanel3.setLayout(new java.awt.GridBagLayout());

        jScrollPane2.setMaximumSize(new java.awt.Dimension(30, 64));
        jScrollPane2.setMinimumSize(new java.awt.Dimension(30, 64));
        jScrollPane2.setPreferredSize(new java.awt.Dimension(30, 64));

        jResetClocksTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Clock", "Reset"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jResetClocksTable.setMaximumSize(new java.awt.Dimension(30, 64));
        jResetClocksTable.setPreferredSize(new java.awt.Dimension(30, 64));
        jResetClocksTable.getTableHeader().setReorderingAllowed(false);
        jScrollPane2.setViewportView(jResetClocksTable);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 220;
        gridBagConstraints.ipady = 82;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        jPanel3.add(jScrollPane2, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 13;
        gridBagConstraints.gridwidth = 2;
        jPanel1.add(jPanel3, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 13;
        gridBagConstraints.ipadx = 20;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        jPanel1.add(jBlankLabel1, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.ipadx = 20;
        jPanel1.add(jBlankLabel2, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.ipadx = 20;
        jPanel1.add(jBlankLabel3, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.ipadx = 20;
        jPanel1.add(jBlankLabel4, gridBagConstraints);

        jClockGuardAtomsLabel.setText("Clock Guard Atoms Conjunction");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 11;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.PAGE_START;
        jPanel1.add(jClockGuardAtomsLabel, gridBagConstraints);

        jClockGuardAtomsComboBox.setMinimumSize(new java.awt.Dimension(100, 20));
        jClockGuardAtomsComboBox.setPreferredSize(new java.awt.Dimension(100, 20));
        jClockGuardAtomsComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jClockGuardAtomsComboBoxActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 11;
        jPanel1.add(jClockGuardAtomsComboBox, gridBagConstraints);

        jMessageLabel.setText("Message");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        jPanel1.add(jMessageLabel, gridBagConstraints);

        jMessageButtonsPanel.setLayout(new java.awt.GridLayout(1, 0));

        jMessageNewButton.setText("New");
        jMessageNewButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMessageNewButtonActionPerformed(evt);
            }
        });
        jMessageButtonsPanel.add(jMessageNewButton);

        jMessageEditButton.setText("Edit");
        jMessageEditButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMessageEditButtonActionPerformed(evt);
            }
        });
        jMessageButtonsPanel.add(jMessageEditButton);

        jMessageDeleteButton.setText("Delete");
        jMessageDeleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMessageDeleteButtonActionPerformed(evt);
            }
        });
        jMessageButtonsPanel.add(jMessageDeleteButton);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        jPanel1.add(jMessageButtonsPanel, gridBagConstraints);

        jDestinationLocationButtonsPanel.setLayout(new java.awt.GridLayout(1, 0));

        jDestLocationNewButton.setText("New");
        jDestLocationNewButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jDestLocationNewButtonActionPerformed(evt);
            }
        });
        jDestinationLocationButtonsPanel.add(jDestLocationNewButton);

        jDestLocationEditButton.setText("Edit");
        jDestLocationEditButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jDestLocationEditButtonActionPerformed(evt);
            }
        });
        jDestinationLocationButtonsPanel.add(jDestLocationEditButton);

        jButton6.setText("Delete");
        jButton6.setEnabled(false);
        jDestinationLocationButtonsPanel.add(jButton6);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        jPanel1.add(jDestinationLocationButtonsPanel, gridBagConstraints);

        jSourceLocationButtonsPanel.setLayout(new java.awt.GridLayout(1, 0));

        jButton7.setText("New");
        jButton7.setEnabled(false);
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });
        jSourceLocationButtonsPanel.add(jButton7);

        jSrcLocationEditButton.setText("Edit");
        jSrcLocationEditButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jSrcLocationEditButtonActionPerformed(evt);
            }
        });
        jSourceLocationButtonsPanel.add(jSrcLocationEditButton);

        jButton9.setText("Delete");
        jButton9.setEnabled(false);
        jSourceLocationButtonsPanel.add(jButton9);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        jPanel1.add(jSourceLocationButtonsPanel, gridBagConstraints);

        jClockGuardsButtonPanel.setLayout(new java.awt.GridLayout(1, 0));

        jClockGuardNewButton.setText("New");
        jClockGuardNewButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jClockGuardNewButtonActionPerformed(evt);
            }
        });
        jClockGuardsButtonPanel.add(jClockGuardNewButton);

        jClockGuardEditButton.setText("Edit");
        jClockGuardEditButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jClockGuardEditButtonActionPerformed(evt);
            }
        });
        jClockGuardsButtonPanel.add(jClockGuardEditButton);

        jClockGuardDeleteButton.setText("Delete");
        jClockGuardDeleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jClockGuardDeleteButtonActionPerformed(evt);
            }
        });
        jClockGuardsButtonPanel.add(jClockGuardDeleteButton);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 11;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        jPanel1.add(jClockGuardsButtonPanel, gridBagConstraints);

        jActionLabel.setFont(new java.awt.Font("Calibri", 1, 14)); // NOI18N
        jActionLabel.setText("New Transition");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 6;
        jPanel1.add(jActionLabel, gridBagConstraints);

        jLabel2.setText("Name");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel1.add(jLabel2, gridBagConstraints);

        jNameTextField.setColumns(10);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel1.add(jNameTextField, gridBagConstraints);

        getContentPane().add(jPanel1, new java.awt.GridBagConstraints());

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jSourceLocationComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jSourceLocationComboBoxActionPerformed
        // TODO add your handling code here:
        if (transition != null) {
            transition.setSrcLocation(getSelectedSrcLocation());
        }
    }//GEN-LAST:event_jSourceLocationComboBoxActionPerformed

    private void jMessageEditButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMessageEditButtonActionPerformed
        // TODO add your handling code here:
        transition.setMessage(getSelectedMessage());
        TransitionObservable transitionObservable = new TransitionObservable(this, TransitionObservable.MODIF_TYPE.MESSAGE);
        JMessageDialog frame = new JMessageDialog(getSelectedMessage(), transitionObservable, messages);
        frame.setVisible(true);
    }//GEN-LAST:event_jMessageEditButtonActionPerformed

    private void jDestLocationEditButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jDestLocationEditButtonActionPerformed
        // TODO add your handling code here:
        transition.setDestLocation(getSelectedDestLocation());
        TransitionObservable transitionObservable = new TransitionObservable(this, TransitionObservable.MODIF_TYPE.DEST_LOCATION);
        JLocationDialogue frame = new JLocationDialogue(getSelectedDestLocation(), transitionObservable, locations);
        frame.setVisible(true);
    }//GEN-LAST:event_jDestLocationEditButtonActionPerformed

    private void jDestLocationNewButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jDestLocationNewButtonActionPerformed
        // TODO add your handling code here:
        TransitionObservable clockGuardObservable = new TransitionObservable(this, TransitionObservable.MODIF_TYPE.DEST_LOCATION);
        JLocationDialogue frame = new JLocationDialogue(clockGuardObservable, locations);
        frame.setVisible(true);

    }//GEN-LAST:event_jDestLocationNewButtonActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton7ActionPerformed

    private void jSrcLocationEditButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jSrcLocationEditButtonActionPerformed
        // TODO add your handling code here:
//        transition.setSrcLocation(getSelectedSrcLocation());
        transition.setSrcLocation(getSelectedSrcLocation());
        TransitionObservable transitionObservable = new TransitionObservable(this, TransitionObservable.MODIF_TYPE.SRC_LOCATION);
        JLocationDialogue frame = new JLocationDialogue(getSelectedSrcLocation(), transitionObservable, locations);
        frame.setVisible(true);
    }//GEN-LAST:event_jSrcLocationEditButtonActionPerformed

    private void jClockGuardNewButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jClockGuardNewButtonActionPerformed
        // TODO add your handling code here:
        TransitionObservable clockGuardObservable = new TransitionObservable(this, TransitionObservable.MODIF_TYPE.CLOCK_GUARD);
        JClockGuardDialogue frame = new JClockGuardDialogue(clockGuardObservable, clocks, transition.getClockGuard());
        frame.setVisible(true);
    }//GEN-LAST:event_jClockGuardNewButtonActionPerformed

    private void jClockGuardEditButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jClockGuardEditButtonActionPerformed
        // TODO add your handling code here:
        TransitionObservable clockGuardObservable = new TransitionObservable(this, TransitionObservable.MODIF_TYPE.CLOCK_GUARD);
        JClockGuardDialogue frame = new JClockGuardDialogue(getSelectedClock(), clockGuardObservable, clocks, transition.getClockGuard());
        frame.setVisible(true);
    }//GEN-LAST:event_jClockGuardEditButtonActionPerformed

    private void jClockGuardAtomsComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jClockGuardAtomsComboBoxActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jClockGuardAtomsComboBoxActionPerformed

    private void jClockGuardDeleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jClockGuardDeleteButtonActionPerformed
        // TODO add your handling code here:
        transition.delConditionAtom(getSelectedClock());
        update(null, null);
    }//GEN-LAST:event_jClockGuardDeleteButtonActionPerformed

    private void jMessageDeleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMessageDeleteButtonActionPerformed
        // TODO add your handling code here:
        Message msg = getSelectedMessage();
        if (!msg.equals(internalAction)) {
            transition.setMessage(internalAction);
        }
        update(null, null);
    }//GEN-LAST:event_jMessageDeleteButtonActionPerformed

    private void jMessageNewButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMessageNewButtonActionPerformed
        // TODO add your handling code here:
        TransitionObservable observable = new TransitionObservable(this, TransitionObservable.MODIF_TYPE.MESSAGE);
        JMessageDialog frame = new JMessageDialog(observable, messages);
        frame.setVisible(true);
    }//GEN-LAST:event_jMessageNewButtonActionPerformed

    private void jOkButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jOkButtonActionPerformed
        // TODO add your handling code here: 
        transition.getResetClocks().clear();
        for (int i = 0; i < resetClocksModel.getRowCount(); i++) {
            if ((Boolean) (resetClocksModel.getValueAt(i, ResetClocksTableModel.STATUS))) {
                transition.getResetClocks().add((Clock) (resetClocksModel.getValueAt(i, ResetClocksTableModel.CLOCK)));
            }
        }

        String symbol = getSymbol();
        if (symbol == null || symbol.isEmpty()) {
            JOptionPane.showMessageDialog(this, "Transition Symbol Required", "Transition Symbol Required", JOptionPane.ERROR_MESSAGE);
            return;
        }

        if (transition.getSrcLocation() == null) {
            JOptionPane.showMessageDialog(this, "Source Location Required", "Source Location Required", JOptionPane.ERROR_MESSAGE);
            return;
        }
        if (transition.getDestLocation() == null) {
            JOptionPane.showMessageDialog(this, "Destination Location Required", "Destination Location Required", JOptionPane.ERROR_MESSAGE);
            return;
        }
        if (transition.getMessage() == null) {
            JOptionPane.showMessageDialog(this, "Transition Message Required", "Transition Message Required", JOptionPane.ERROR_MESSAGE);
            return;
        }

//        if (!(repository.getMessage(symbol) == null)) {
        if (isNameDuplicated(symbol)) {
            JOptionPane.showMessageDialog(this, "Transition Symbol Duplicated", "Transition Symbol Duplicated", JOptionPane.ERROR_MESSAGE);
            return;
        }
        //We must verify that this transition does not exist with an other name
        if (transitions.contains(transition)) {
            JOptionPane.showMessageDialog(this, "Transition Duplicated", "Transition Duplicated", JOptionPane.ERROR_MESSAGE);
            return;
        }

        // add the new symbol to repository
        if (newNotEdit) {
            //transition.setDestLocation(location);
            transition.setName(symbol);
            repository.addTransition(transition);
            JOptionPane.showMessageDialog(this, "Transition Created");
        } else {
            repository.renameTransition(transition, symbol);
            JOptionPane.showMessageDialog(this, "Transition Changed");
        }
        observable.setResult(transition);
        observable.notifyTransitionChange();
        dispose();



    }//GEN-LAST:event_jOkButtonActionPerformed
    private String getSymbol() {
        return jNameTextField.getText();
    }

    private boolean isNameDuplicated(String name) {
        boolean res = false;
        if (name.compareTo(transition.getName()) == 0) {
            return false;
        }
        for (Transition transition : transitions) {
            if (transition.getName().compareTo(name) == 0) {
                res = true;
                break;
            }
        }
        return res;
    }

    private void jRestoreButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRestoreButtonActionPerformed
        // TODO add your handling code here:
        restore();
    }//GEN-LAST:event_jRestoreButtonActionPerformed

    private void jDestinationLocationComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jDestinationLocationComboBoxActionPerformed
        // TODO add your handling code here:
        if (transition != null) {
            transition.setDestLocation(getSelectedDestLocation());
        }
    }//GEN-LAST:event_jDestinationLocationComboBoxActionPerformed

    private void jMessageComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMessageComboBoxActionPerformed
        // TODO add your handling code here:
        if (transition != null) {
            transition.setMessage(getSelectedMessage());
        }
        if (getSelectedMessage() != null) {
            jMessageEditButton.setEnabled(!getSelectedMessage().equals(internalAction));
        }
    }//GEN-LAST:event_jMessageComboBoxActionPerformed

    private void jCancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCancelButtonActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_jCancelButtonActionPerformed

    private ClockConditionAtom getSelectedClock() {
        return (ClockConditionAtom) jClockGuardAtomsComboBox.getSelectedItem();
    }

    private Message getSelectedMessage() {
        return (Message) jMessageComboBox.getSelectedItem();
    }

    private Location getSelectedDestLocation() {
        return (Location) jDestinationLocationComboBox.getSelectedItem();
    }

    private Location getSelectedSrcLocation() {
        return (Location) jSourceLocationComboBox.getSelectedItem();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JTransitionDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JTransitionDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JTransitionDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JTransitionDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JTransitionDialog(null, null).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jActionLabel;
    private javax.swing.JLabel jBlankLabel1;
    private javax.swing.JLabel jBlankLabel2;
    private javax.swing.JLabel jBlankLabel3;
    private javax.swing.JLabel jBlankLabel4;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton9;
    private javax.swing.JButton jCancelButton;
    private javax.swing.JComboBox jClockGuardAtomsComboBox;
    private javax.swing.JLabel jClockGuardAtomsLabel;
    private javax.swing.JButton jClockGuardDeleteButton;
    private javax.swing.JButton jClockGuardEditButton;
    private javax.swing.JButton jClockGuardNewButton;
    private javax.swing.JPanel jClockGuardsButtonPanel;
    private javax.swing.JPanel jClockResetButtonsPanel;
    private javax.swing.JButton jDestLocationEditButton;
    private javax.swing.JButton jDestLocationNewButton;
    private javax.swing.JPanel jDestinationLocationButtonsPanel;
    private javax.swing.JComboBox jDestinationLocationComboBox;
    private javax.swing.JLabel jDestinationLocationLabel;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jMessageButtonsPanel;
    private javax.swing.JComboBox jMessageComboBox;
    private javax.swing.JButton jMessageDeleteButton;
    private javax.swing.JButton jMessageEditButton;
    private javax.swing.JLabel jMessageLabel;
    private javax.swing.JButton jMessageNewButton;
    private javax.swing.JTextField jNameTextField;
    private javax.swing.JButton jOkButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JLabel jResetClocksLabel;
    private javax.swing.JTable jResetClocksTable;
    private javax.swing.JButton jRestoreButton;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JPanel jSourceLocationButtonsPanel;
    private javax.swing.JComboBox jSourceLocationComboBox;
    private javax.swing.JLabel jSourceLocationLabel;
    private javax.swing.JButton jSrcLocationEditButton;
    // End of variables declaration//GEN-END:variables

    @Override
    public void update(Observable o, Object o1) {
        TransitionObservable obs = (TransitionObservable) o;
        switch (obs.getModifType()) {
            case MESSAGE: {
                transition.setMessage((Message) (obs.getResult()));
                break;
            }
            case SRC_LOCATION: {
                transition.setSrcLocation((Location) (obs.getResult()));
                break;
            }
            case DEST_LOCATION: {
                transition.setDestLocation((Location) (obs.getResult()));
                break;
            }
            case CLOCK_GUARD: {
                //transition.set((Location)(obs.getResult()));                
                break;
            }
            case RESET_CLOCK: {
                break;
            }
        }
        fillInComponents();
    }

    public Transition getTransition() {
        return transition;
    }

    public void setTransition(Transition transition) {
        this.transition = transition;
    }
}

class ResetClocksTableModel extends AbstractTableModel {

    String[] columns = {"Clock", "Reset"};
    Object[][] clocks;
    public final static int CLOCK = 0;
    public final static int STATUS = 1;

    public ResetClocksTableModel(int rows) {
        clocks = new Object[rows][2];
    }

    public int getRowCount() {
        return clocks.length;
    }

    public void setClocks(Object[][] clocks) {
        this.clocks = clocks;
    }

    public int getColumnCount() {
        return columns.length;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        return clocks[rowIndex][columnIndex];
    }

    public void setValueAt(Object value, int rowIndex, int columnIndex) {
        clocks[rowIndex][columnIndex] = value;
    }

    @Override
    public String getColumnName(int column) {
        return columns[column];
    }

    public boolean isCellEditable(int row, int col) {
        return col == STATUS;
    }
    //
    // This method is used by the JTable to define the default
    // renderer or editor for each cell. For example if you have
    // a boolean data it will be rendered as a check box. A
    // number value is right aligned.
    //

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return clocks[0][columnIndex].getClass();
    }
}
